package com.wh.custom.controller;

import com.wh.basic.vo.AjaxResult;
import com.wh.custom.service.ICustomCustomersourceService;
import com.wh.custom.pojo.CustomCustomersource;
import com.wh.custom.dto.CustomCustomersourceQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/customerSource")
public class CustomSourceController {

    @Autowired
    public ICustomCustomersourceService customCustomersourceService;

    /**
     * 接口：添加或修改
     * @param customCustomersource 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody CustomCustomersource customCustomersource) {
        if ( customCustomersource.getId() != null){
            customCustomersourceService.updateById(customCustomersource);
        }else{
            customCustomersourceService.save(customCustomersource);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        customCustomersourceService.removeById(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        customCustomersourceService.removeByIds(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     * @param id
     */
    @GetMapping("/{id}")
    public CustomCustomersource findOne(@PathVariable("id") Long id) {
        return customCustomersourceService.getById(id);
    }


    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    public List<CustomCustomersource> findAll() {
        return customCustomersourceService.list();
    }


    /**
     * 接口：分页查询或高级查询
     * @param customCustomersourceQueryDto 查询对象
     * @return IPage<CustomCustomersource> 分页对象
     */
    @PostMapping
    public IPage<CustomCustomersource> findByPage(@RequestBody CustomCustomersourceQueryDto customCustomersourceQueryDto) {
        return customCustomersourceService.findByPage(customCustomersourceQueryDto);
    }
}
